<template>
  <div class="roleManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              placeholder="请输入查询条件"
              clearable
            />
          </el-form-item>
          <el-form-item label="授权状态" prop="authstat">
            <el-select v-model="queryForm.authstat" placeholder="请选择">
              <el-option
                v-for="item in authstatlist"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12"></vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column prop="name" label="产品名称"></el-table-column>
      <el-table-column
        prop="pkey"
        show-overflow-tooltip
        label="产品KEY"
      ></el-table-column>
      <el-table-column prop="nodetypetxt" label="节点类型"></el-table-column>
      <el-table-column prop="ctime" label="创建时间"></el-table-column>
      <el-table-column prop="authstat" label="授权状态">
        <template #default="scope">
          <p v-if="scope.row.authstat == '0'">
            <el-tag type="danger">未授权</el-tag>
          </p>
          <p v-if="scope.row.authstat == '1'">
            <el-tag type="">已授权通过</el-tag>
          </p>
          <p v-if="scope.row.authstat == '2'">
            <el-tag type="warning">授权未通过</el-tag>
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="beytype" label="所属产品"></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-button type="primary" @click="handleEdit(row, '1')">
            授权通过
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
  import { getProList, productAuth } from '@/api/Product'
  export default {
    name: 'RoleManagement2',
    components: {},
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          name: '',
          authstat: '',
        },
        authstatlist: [
          { id: '0', name: '未授权' },
          { id: '1', name: '已授权通过' },
          { id: '2', name: '授权未通过' },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleEdit(row, status) {
        this.$baseConfirm('你确定当前操作吗', null, async () => {
          const { msg } = await productAuth({ id: row.id, status: status })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getProList(this.queryForm)
        this.list = data.list
        this.total = data.pageInfo.data.totalRows
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
